package com.woniu.elderly.serve.controller;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.common.dto.ServePersonDTO;
import com.woniu.common.entity.Result;
import com.woniu.common.entity.ServePerson;
import com.woniu.elderly.serve.service.ServePersonService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/serveperson")
@Slf4j
public class ServePersonController {
    @Autowired
    private ServePersonService servePersonService;

    //查询全部服务人员有参数
    @PostMapping("/{pageIndex}/{pageSize}")
    public Result<Page<ServePerson>> findAll(@PathVariable("pageIndex") int pageIndex,
                                             @PathVariable("pageSize") int pageSize,
                                             @RequestBody ServePersonDTO servePersonDTO){
        log.info("serveperson-findAll:pageIndex={},pageSize={},servePersonDTO={}", pageIndex, pageSize, servePersonDTO);
        return Result.ok(servePersonService.findAll(pageIndex, pageSize, servePersonDTO));
    }

    //查询全部服务人员没有参数
    @GetMapping("/")
    public Result<List<ServePerson>> findAll(){
        return Result.ok(servePersonService.findAll());
    }

    //根据id查询服务人员
    @GetMapping("/{id}")
    public Result<ServePerson> findById(@PathVariable("id") int id){
        log.info("serveperson-findById:id={}", id);
        return Result.ok(servePersonService.findById(id));
    }

    //新增服务人员
    @PostMapping("/")
    public Result<String> add(@RequestBody ServePerson servePerson){
        log.info("serveperson-add:serverPerson={}", servePerson);
        return Result.ok(servePersonService.add(servePerson));
    }

    //修改服务人员
    @PutMapping("/")
    public Result<Void> modify(@RequestBody ServePerson servePerson){
        log.info("serveperson-modify:serverPerson={}", servePerson);
        servePersonService.modify(servePerson);
        return Result.ok();
    }

    //离职申请
    @GetMapping("/apply/{id}/{cause}")
    public Result<Void> apply(@PathVariable("id") int id, @PathVariable("cause") String cause){
        log.info("serveperson-apply:id={},cause={}", id, cause);
        servePersonService.apply(id, cause);
        return Result.ok();
    }

    //撤销申请
    @PutMapping("/revocation/")
    public Result<Void> revocation(@RequestBody ServePerson servePerson){
        log.info("serveperson-revocation:servePerson={}", servePerson);
        servePersonService.revocation(servePerson);
        return Result.ok();
    }

    //删除服务人员
    @DeleteMapping("/{id}")
    public Result<Void> remove(@PathVariable("id") int id){
        log.info("serveperson-remove:id={}", id);
        servePersonService.remove(id);
        return Result.ok();
    }

    //随机抽一位服务人员id
    @GetMapping("/random/{serveType}")
    public Result<Integer> random(@PathVariable("serveType") String serveType){
        log.info("serveperson-random:serveType={}", serveType);
        return Result.ok(servePersonService.random(serveType));
    }

    //服务人员完成服务
    @PutMapping("/finish/")
    public Result<Void> finish(@RequestBody ServePerson servePerson) {
        log.info("serveperson-finish:servePerson={}", servePerson);
        //servePersonService.finish(servePerson);
        return Result.ok();
    }
}
